/*
 * Copyright 2015 MingxingWang

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

 *     http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.audaque.graphdb

import org.apache.spark.graphx._

class Edge(val srcId: VertexId,
           val dstId: VertexId,
           var prop: Property = null)
  extends Serializable {

  def apply(index: Int): Any = if (prop == null) null else prop(index)

  def apply(name: String): Any = if (prop == null) null else prop(name)

  def getProperty(index: Int): Any = apply(index)

  def getProperty(name: String): Any = apply(name)

  def canEqual(other: Any): Boolean = other.isInstanceOf[Edge]

  override def equals(other: Any): Boolean = other match {
    case that: Edge =>
      (that canEqual this) &&
        srcId == that.srcId &&
        dstId == that.dstId
    case _ => false
  }

  def debug() = {
    print("src id:" + srcId + ", dst id:" + dstId+", ")
    if (prop != null) {
      prop.debug
    }
    println()
  }
}

object Edge {
  def apply(srcId: VertexId, dstId: VertexId): Edge = new Edge(srcId, dstId)

  def apply(srcId: VertexId, dstId: VertexId, prop: Property): Edge = new Edge(srcId, dstId, prop)
}
